<?php
class Administrator_gallery_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }		
	
	/*Content Management*/
	function content_management_add()
	{
		$is_publish = $this->input->post('is_publish');
		$gallery_publish_date = $this->input->post('gallery_publish_date');
		$is_main_hilight = $this->input->post('is_main_hilight');
		$is_type_hilight = $this->input->post('is_type_hilight');		
		$gallery_category = $this->input->post('gallery_category');
		$gallery_name = $this->input->post('gallery_name');
		$gallery_brief = $this->input->post('gallery_brief');
		$gallery_description = $this->input->post('gallery_description');
		$gallery_tag = $this->input->post('gallery_tag');
		$gallery_date = $this->input->post('gallery_date');
		$uploadify_store = explode(',',$this->input->post('uploadify_store'));
		$image_watermark_disable = $this->input->post('image_watermark_disable');
		$datetime = date('Y-m-d H:i:s');
		
		//Insert gallery
		$data = array(
			'gallery_category' => $gallery_category,
			'gallery_publish_date' => convertDatetime($gallery_publish_date),
			'gallery_name' => $gallery_name,
			'gallery_brief' => $gallery_brief,
			'gallery_description' => $gallery_description,
			'gallery_date' => convertDatetime($gallery_date),
			'is_publish' => ''.$is_publish.'',
			'is_main_hilight' => ''.$is_main_hilight.'',
			'is_type_hilight' => ''.$is_type_hilight.'',			
			'gallery_tag' => ''.$gallery_tag.'',			
			'update_by' => $this->session->userdata('username'),
			'create_date' => $datetime,
			'update_date' => $datetime
		);
		$this->db->insert('gallery',$data);
		$gallery_id = $this->db->insert_id();
			
		//Insert gallery_image
		$dataImage = array();
		if(!empty($uploadify_store[0])) foreach($uploadify_store as $key => $value):
			//Resize Image
			$data = imageResize(array(
				'id' => $gallery_id,
				'source_image' => $this->temp_folder.$value,
				'new_path' => $this->upload_folder.'gallery/images/',
				'dimension' => array(												
					array(
						'width' => 120,
						'height' => 120
					)					
				),
				'watermark' => ($image_watermark_disable == false ? $this->upload_folder.'watermark/watermark.png':'')
			));			
			
			//delete temp
			@unlink($this->temp_folder.$value);
			
			$dataImage[] = array(
				'gallery_id' => $gallery_id,
				'gallery_file_name' => $data->file_name,
				'gallery_file_ext' => $data->file_ext
			);	
		endforeach;
		if(!empty($dataImage)) $this->db->insert_batch('gallery_image',$dataImage);
		
		//Delete Memcache
		$this->memcache->delete('dataGalleryHilight');
		$this->memcache->delete('dataGalleryUpdate');
		$this->memcache->delete('dataModuleGalleryHot');
		$this->memcache->delete('dataModuleGalleryNew');
		
		//Redirect to Edit Page
		redirect('gallery/content_management/edit/'.$gallery_id);		
	}
	
	function content_management_edit($gallery_id)
	{
		$is_publish = $this->input->post('is_publish');
		$gallery_publish_date = $this->input->post('gallery_publish_date');
		$is_main_hilight = $this->input->post('is_main_hilight');
		$is_type_hilight = $this->input->post('is_type_hilight');		
		$gallery_category = $this->input->post('gallery_category');
		$gallery_name = $this->input->post('gallery_name');
		$gallery_brief = $this->input->post('gallery_brief');
		$gallery_description = $this->input->post('gallery_description');
		$gallery_tag = $this->input->post('gallery_tag');
		$gallery_date = $this->input->post('gallery_date');
		$uploadify_store = explode(',',$this->input->post('uploadify_store'));
		$image_watermark_disable = $this->input->post('image_watermark_disable');
		$datetime = date('Y-m-d H:i:s');

		//Insert to gallery
		$data = array(
			'gallery_category' => $gallery_category,
			'gallery_publish_date' => convertDatetime($gallery_publish_date),
			'gallery_name' => $gallery_name,
			'gallery_brief' => $gallery_brief,
			'gallery_description' => $gallery_description,
			'gallery_date' => convertDatetime($gallery_date),
			'is_publish' => ''.$is_publish.'',
			'is_main_hilight' => ''.$is_main_hilight.'',
			'is_type_hilight' => ''.$is_type_hilight.'',
			'gallery_tag' => ''.$gallery_tag.'',			
			'update_by' => $this->session->userdata('username'),
			'update_date' => $datetime
		);
		$this->db->update('gallery',$data,array('gallery_id' => $gallery_id));
		
		//Insert gallery_image
		$dataImage = array();
		if(!empty($uploadify_store[0])) foreach($uploadify_store as $key => $value):
			//Resize Image
			$data = imageResize(array(
				'id' => $gallery_id,
				'source_image' => $this->temp_folder.$value,
				'new_path' => $this->upload_folder.'gallery/images/',
				'dimension' => array(											
					array(
						'width' => 120,
						'height' => 120
					)				
				),
				'watermark' => ($image_watermark_disable == false ? $this->upload_folder.'watermark/watermark.png':'')
			));		
			
			//delete temp
			@unlink($this->temp_folder.$value);
			
			$dataImage[] = array(
				'gallery_id' => $gallery_id,
				'gallery_file_name' => $data->file_name,
				'gallery_file_ext' => $data->file_ext
			);	
		endforeach;
		if(!empty($dataImage)) $this->db->insert_batch('gallery_image',$dataImage);

		//Delete Memcache
		$this->memcache->delete('dataGalleryHilight');
		$this->memcache->delete('dataGalleryUpdate');
		$this->memcache->delete('dataModuleGalleryHot');
		$this->memcache->delete('dataModuleGalleryNew');		
	}	
	
	function ajax_update_image()
	{
		$gallery_image_id = $this->input->post('gallery_image_id');
		if(!empty($gallery_image_id)) foreach($gallery_image_id as $key => $value):
			$this->db->update('gallery_image',array('gallery_image_order' => $key),array('gallery_image_id' => $value));
		endforeach;
		exit;
	}
	
	function ajax_delete_image($gallery_image_id)
	{
		$dataImage = $this->db->get_where('gallery_image',array('gallery_image_id' => $gallery_image_id),1)->row();
		$this->db->delete('gallery_image',array('gallery_image_id' => $gallery_image_id));
		//Delete All Image
		$map = directory_map($this->upload_folder.'gallery/images/', 1);
		foreach($map as $folder):
			@unlink($this->upload_folder.'gallery/images/'.$folder.'/'.$dataImage->gallery_file_name.'.'.$dataImage->gallery_file_ext);
		endforeach;
		exit;
	}	
	
	function content_management_delete()
	{
		$id = $this->input->post('id');
		$map = directory_map($this->upload_folder.'gallery/images/', 1);
		foreach($id as $value):
			$dataImage = $this->db->get_where('gallery_image',array('gallery_id' => $value))->result();
			$tables = array('gallery','gallery_image','gallery_click_stat');
			$this->db->where('gallery_id', $value);
			$this->db->delete($tables);
			//Delete All Image
			foreach($map as $folder):
				foreach($dataImage as $items):
					@unlink($this->upload_folder.'gallery/images/'.$folder.'/'.$items->gallery_file_name.'.'.$items->gallery_file_ext);
				endforeach;
			endforeach;					
		endforeach;
		
		//Delete Memcache
		$this->memcache->delete('dataGalleryHilight');
		$this->memcache->delete('dataGalleryUpdate');
		$this->memcache->delete('dataModuleGalleryHot');
		$this->memcache->delete('dataModuleGalleryNew');		
	}
	/*End Content Management*/
}

?>